﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameObjectsAndEquipment;
using GameObjectsAndEquipment.Weapons;

namespace StateMachine.GameStates
{
    public class EquipmentState : GameState
    {
        private SpriteFont font;
        private SpriteFont font2;
        private Equipment equip;
        private VampireRider player;
        private List<TextObject> items;

        public EquipmentState(int screenWidth, int screenHeight, VampireRider player)
            : base(screenWidth,screenHeight)
        {
            this.equip = player.Equipment;
            this.player = player;
            items = new List<TextObject>();

            
        }

        public override void LoadContent(ContentManager content)
        {
            base.LoadContent(content);

            font2 = content.Load<SpriteFont>("basicS");
            font = content.Load<SpriteFont>("basic");

            for (int i = 0; i < equip.BackpackItems.Count; i++)
            {
                items.Add(new TextObject(new Vector2(screenWidth / 2 + 30, 100 + i * 30), equip.BackpackItems[i].name, font2, Color.White));
            }
        }

        public override void HandleInput(GameStateManager manager, GameTime gameTime)
        {
            base.HandleInput(manager, gameTime);

            KeyboardState newState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();

            if (newState.IsKeyDown(Keys.Escape))
            {
                manager.PopState();
            }

            //check if mouse is over a menu entry
            for (int i = items.Count-1; i >=0 ; i--)
            {
                if (CheckIfMouseOver(items[i], mouseState))
                {
                    items[i].Color = Color.Yellow;
                    if (mouseState.LeftButton == ButtonState.Pressed)
                    {
                        SolveMenuClick(i);
                    }
                }
                else
                {
                    items[i].Color = Color.Red;
                }
            }

            oldState = newState;
        }

        public override void Update(GameStateManager manager, GameTime gameTime)
        {
            base.Update(manager, gameTime);
            HandleInput(manager, gameTime);

        }

        public override void Draw(GameStateManager manager, GameTime gameTime)
        {
            base.Draw(manager, gameTime);

            manager.Renderer.DrawText(new TextObject(new Vector2(screenWidth/2-75,20),"Equipment",font,Color.White),font);
            manager.Renderer.DrawText(new TextObject(new Vector2(20,60), "Current Weapon:", font, Color.White), font);
            manager.Renderer.DrawText(new TextObject(new Vector2(30, 90), equip.Weapon.WeaponType, font2, Color.White), font2);

            manager.Renderer.DrawText(new TextObject(new Vector2(20, 140), "Ammo:", font, Color.White), font);
            manager.Renderer.DrawText(new TextObject(new Vector2(30,170), equip.Weapon.Bullets.ToString(), font2, Color.White), font2);


            manager.Renderer.DrawText(new TextObject(new Vector2(screenWidth/2+20, 60), "Backpack:", font, Color.White), font);

            for (int i = 0; i < items.Count; i++)
            {
                manager.Renderer.DrawText(items[i], font2);
            }
            

        }

        private Boolean CheckIfMouseOver(TextObject textObject, MouseState mouseState)
        {
            if (mouseState.X > textObject.X && mouseState.X < textObject.X + textObject.Width)
            {
                if (mouseState.Y > textObject.Y && mouseState.Y < textObject.Y + textObject.Height)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        private void SolveMenuClick(int index)
        {
            equip.BackpackItems[index].Use();
            equip.BackpackItems.RemoveAt(index);
            items.RemoveAt(index);
        }
    }
}
